package com.party.mobile.web.controller.integral;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.commune.CMoment;
import com.party.core.model.commune.MomentType;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.PartnerMember;
import com.party.core.service.commune.ICMomentService;
import com.party.core.service.dynamic.IDypicsService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.member.IPartnerMemberService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.core.model.member.CMomentOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @program: party
 * @description: 动态控制器
 * @author: Hua Qunhao
 * @create: 2019-12-23 16:27
 **/
@Controller
@RequestMapping(value = "/moment")
public class CMomentController {
    protected static Logger logger = LoggerFactory.getLogger(CMomentController.class);

    @Autowired
    private ICMomentService momentService;
    @Autowired
    private IPartnerMemberService partnerMemberService;
    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private IDypicsService dypicsService;
    @Autowired
    private IMemberService memberService;


    /**
     * @Description: 动态列表
     * @Param: [pmId, page]
     * @return: com.party.mobile.web.dto.AjaxResult
     * @Author: Hua Qunhao
     * @Date: 2019/12/23
     */
    @ResponseBody
    @RequestMapping("list")
    public AjaxResult list(String pmId, Page page) {
        if (StringUtils.isEmpty(pmId)) {
            logger.error("动态列表----pmId为空");
            return AjaxResult.error(100, "动态列表----pmId为空");
        }
        try {
            PartnerMember partnerMember = partnerMemberService.get(pmId);
            if (partnerMember == null) {
                logger.error("动态列表----partnerMember为空");
                return AjaxResult.error(100, "动态列表----partnerMember为空");
            }
            if (PartnerMember.IS_MAIN_TYPE != partnerMember.getPartnerType()) {
                partnerMember = partnerMemberService.getMainOrg(partnerMember.getMemberId(), partnerMember.getPartnerId());
            }
            MemberGroup memberGroup = memberGroupService.get(partnerMember.getPartnerId());
            List<CMomentOutput> momentOutputs = Lists.newArrayList();
            Map<String, Object> mmp = Maps.newHashMap();
            List<String> orgIds = Lists.newArrayList();
            orgIds.add(memberGroup.getId());
            orgIds.add(memberGroup.getPartnerId());
            mmp.put("orgIds", orgIds);
            List<CMoment> cMoments = momentService.mobileListPage(mmp, page);
            momentOutputs = LangUtils.transform(cMoments, cMoment -> {
                CMomentOutput output = CMomentOutput.transform(cMoment);

                //封面图
                List<String> pics = Lists.newArrayList();
                if (StringUtils.isNotEmpty(cMoment.getPics())) {
                    String[] picList = cMoment.getPics().split("\\|");
                    pics = Arrays.asList(picList);
                }
                output.setPicList(pics);

                //发布者头像url
                String avatarUrl = null;
                //发布者
                String avatar = null;

                if (StringUtils.isNotEmpty(cMoment.getMemberId())) {
                    Member member = memberService.get(cMoment.getMemberId());
                    if(null!=member){
                        avatarUrl = member.getLogo();
                        avatar = member.getRealname();
                    }
                } else {
                    MemberGroup memberGroup_avatar = memberGroupService.get(cMoment.getCreateBy());
                    if(memberGroup_avatar!=null){
                        avatarUrl = memberGroup_avatar.getLogo();
                        avatar = memberGroup_avatar.getRealname();
                    }
                }
                output.setAvatarUrl(avatarUrl);
                output.setAvatar(avatar);
                if (MomentType.MEMBER_JOIN.getCode() == output.getMomentType() || MomentType.ADD_INTEGRAL.getCode() == output.getMomentType()) {
                    String[] split = cMoment.getBusinessIds().split(",");
                    Set<String> set = new HashSet<String>(Arrays.asList(split));
                    List<Map<String, Object>> maps = memberService.batchMemberList(set);
                    output.setDatas(maps);
                }
                return output;
            });
            return AjaxResult.success(momentOutputs, page);
        } catch (BusinessException e) {
            return AjaxResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("动态列表----", e);
            return AjaxResult.error(100, "动态列表----");
        }
    }
}
